Ext.namespace('pages.main');

pages.main.Main = function(config) {
	Ext.apply(this, config);
	this.initUIComponents();
	pages.main.Main.superclass.constructor.call(this);
};

Ext.extend(pages.main.Main, Ext.Panel, {
	/**
	 * 构造函数
	 */
	initUIComponents : function() {
		
		this.northPanel = new Ext.Panel({
			region : 'north',
			height : 80,
			html :"	<div align='center'><span style='position:absolute;top:25px;font-family:黑体,宋体,微软雅黑;color:white;font-size:25px;letter-spacing:3px;left:270px'>红点e贷云服务管理系统</span><img src='head.jpg' width='100%' height=80/><span style='position: absolute; left:900px;font-family:微软雅黑;color:white;font-size:16px;height:35px;   top: 30px;display:block;background:url(2222.png)repeat-x 0px -30px;'><img style='height: 35px;margin-top:-4px;float: left;margin-left:-15px;' src='1111.png' /><div style='float:left;margin-top:4px;margin-left:10px;'><img style='vertical-align: text-top; height:20px;' src='yh.png' />"+gyh+"&nbsp;&nbsp;&nbsp;<img style='vertical-align: text-top;height:20px;' src='mm.png' /><a style='cursor:pointer;' onclick='changePssword()'>修改密码</a>&nbsp;&nbsp;&nbsp;<a style='cursor:pointer;' onclick='changeSys()'>切换系统</a>&nbsp;&nbsp;&nbsp;<a style='cursor:pointer;' onclick='twoChangeSys()'>切换系统2</a><img style='vertical-align: text-top;height:20px;' src='zx.png' /><a style='cursor:pointer;'onclick='onLoginOUt()'>注销</a></div><img style='height: 35px;margin-top:-3px;float: right;margin-right:-14px;' src='33333.png' /></span></div>"
							
//			html : "<iframe src='top.jsp' id='top' name='top' width='100%' height=80 frameborder='0'></iframe>"
		});
		//
		
		this.westPanel = this.tree = new Ext.tree.TreePanel({
			region : "west",
			title : '菜单',
			id:"mainMenu",
			width : 200,
			border : false,
			split : true,
			collapsible : true,
			loader : new Ext.tree.TreeLoader({
				baseParams : {
					nodeId : '-1'
				},
				dataUrl : baseUrl + '/menu/queryMenuList.do'
			}),
			root : new Ext.tree.AsyncTreeNode({
				id : '-1',
				draggable : false,
				expanded : false,
				text : ''
			}),
			autoScroll : true,
			animate : false,
			border : false,
			enableDD : false,
			containerScroll : true,
			singleExpand : false,
			draggable : false,
			rootVisible : false,
			listeners : {
				'render' : function() {
					this.tree.getRootNode().expand();
				}.createDelegate(this)
				,'beforeload': function(node, e){
					this.tree.getLoader().baseParams.nodeId = node.id;
				}.createDelegate(this)
				,'click' : this.onTreeNodeClick.createDelegate(this)
			}
		});
		
		this.tabPanel = new Ext.TabPanel({
			id: 'tabPanel',
			region : 'center',
			margin : '0 0 0 0',
			defaults : {
				autoScroll:true,
				layout : 'fit'
			},
			enableTabScroll : true,
			layoutOnTabChange : true,
			plugins: new Ext.ux.TabCloseMenu(),
			border : false,
			activeTab : 0,
			plain : true,
			listeners : {
				'tabchange' : function(tabPanel,panel){
				}
			}
		});

		
		Ext.apply(this, {
			closeAction : 'close',
			layout : 'border',
			border : false,
			width : 1360,
			height : 670,
			listeners : {
				'render' : this.initActiveTab.createDelegate(this)
			},
			items : [this.northPanel,this.westPanel,this.tabPanel]
		})
	},

	initActiveTab : function(){
		var fullUrl = baseUrl + "/pages/main.jsp";
		this.tabPanel.add({
						title : '首页',
			    		id : 'main_index',
			    		layout : 'fit',
			    		html : "<iframe src='"+fullUrl+"' id='index' name='index' width='100%' height='99%' frameborder='0'></iframe>"
					}).show();
		
	},
	onTreeNodeClick : function(node, e){
		var pageId = node.attributes.id;
		var pageName = node.attributes.text;
		var pageUrl = node.attributes.url;
		var pageParams = node.attributes.params;
		var fullUrl = baseUrl + pageUrl;
		fullUrl = fullUrl + "?pageId=" + pageId + "&"+pageParams;
		if(node.leaf){
			var itemPanel = this.tabPanel.getItem("TAB"+pageId);
			if(!itemPanel){
				this.tabPanel.add({
						id : "TAB" + pageId,
						title : pageName,
						layout : 'fit',
						closable : true,
						listeners : {
							'activate' : function(panel){
							}
						},
						html : "<iframe src='"+fullUrl+"' id='"+pageId+"' name='"+pageName+"' width='100%' height='99%' frameborder='0'></iframe>"
					}).show();
				document.getElementById(pageId).src = fullUrl;
			}
			this.tabPanel.setActiveTab("TAB"+pageId);
		}
	}
});

function onLoginOUt(){
	Ext.MessageBox.confirm("确认框","确定退出吗？",function(data){
		if(data=="yes"){
			window.location.href = baseUrl + "/j_spring_security_logout";
		}else{
			return;
		}
	});
}
function changePssword(){
	Ext.MessageBox.confirm("确认框","确定修改密码吗？",function(data){
		if(data=="yes"){
			var win = new pages.main.ChanagePassword({
				userInfo:{
					gyh:gyh
				}
			});
			win.show();
		}else{
			return;
		}
	});
}

function changeSys(){
	
	window.location.href = baseUrl + "/asset/main.htm";
}

function twoChangeSys(){
	window.location.href = baseUrl + "/asset/mainNew.htm";
}

Ext.ux.TabCloseMenu = function(){
    var tabs, menu, ctxItem;
    this.init = function(tp){
        tabs = tp;
        tabs.on('contextmenu', onContextMenu);
    }

    function onContextMenu(ts, item, e){
        if(!menu){ // create context menu on first right click
            menu = new Ext.menu.Menu([{
                id: tabs.id + '-close',
                text: '关闭该页',
                handler : function(){
                    tabs.remove(ctxItem);
                }
            },{
                id: tabs.id + '-close-others',
                text: '关闭其他页',
                handler : function(){
                    tabs.items.each(function(item){
                        if(item.closable && item != ctxItem){
                            tabs.remove(item);
                        }
                    });
                }
            },{
                id: tabs.id + '-close-all',
                text: '关闭所有页',
                handler : function(){
                    tabs.items.each(function(item){
                        if(item.closable){
                            tabs.remove(item);
                        }
                    });
                }
            },{
                id: tabs.id + '-close-all',
                text: '取 消'
            }]);
        }
        ctxItem = item;
        var items = menu.items;
        items.get(tabs.id + '-close').setDisabled(!item.closable);
        var disableOthers = true;
        tabs.items.each(function(){
            if(this != item && this.closable){
                disableOthers = false;
                return false;
            }
        });
        items.get(tabs.id + '-close-others').setDisabled(disableOthers);
        menu.showAt(e.getPoint());
    }
};